HOME/Articles/

python aws lambda modules (1)

Article Outline

Example Python program python-aws-lambda-modules (1).py Python version 3.x or newer. To check the Python version use:

python --version

Modules

  • from future import print_function
  • import imp, sys, os, re, time, json
  • import pkgutil, importlib
  • import platform
  • check that this extension can be imported

  • import(m)
  • imported_module = importlib.import_module(name)
  • ver = getattr(imported_module, 'version', '')

Methods

  • def get_suffix(file):
  • def getmodules(p):
  • def getpath():
  • def cb(p, site_package_path=os.path.abspath(p)):
  • def cb(p, cwd=os.path.normcase(os.getcwd())):
  • def lambda_handler(event, context):
  • def cb(m):
  • def less_verbose():

Code

Python example

# Locate all standard modules present in the AWS Lambda environment
#
# To launch call the "lambda_handler" function in Lambda
#
# Based on listmodules.py written by Fredrik Lundh, January 2005
# http://svn.python.org/projects/python/tags/r252/Doc/tools/listmodules.py


from __future__ import print_function
import imp, sys, os, re, time, json
import pkgutil, importlib

identifier = "python-%s-%s" % (sys.version[:3], sys.platform)
timestamp = time.strftime("%Y%m%dT%H%M%SZ", time.gmtime(time.time()))

# known test packages
TEST_PACKAGES = "test.", "bsddb.test.", "distutils.tests."

try:
    import platform
    platform = platform.platform()
except:
    platform = None # unknown

suffixes = imp.get_suffixes()

def get_suffix(file):
    for suffix in suffixes:
        if file[-len(suffix[0]):] == suffix[0]:
            return suffix
    return None


def getmodules(p):
    # get modules in a given directory
    modules = {}
    for f in os.listdir(p):
        f = os.path.join(p, f)
        if os.path.isfile(f):
            m, e = os.path.splitext(f)
            suffix = get_suffix(f)
            if not suffix:
                continue
            m = os.path.basename(m)
            if re.compile("(?i)[a-z_]\w*$").match(m):
                if suffix[2] == imp.C_EXTENSION:
                    # check that this extension can be imported
                    try:
                        __import__(m)
                    except ImportError:
                        continue
                modules[m] = f
        elif os.path.isdir(f):
            m = os.path.basename(f)
            if os.path.isfile(os.path.join(f, "__init__.py")):
                for mm, f in getmodules(f).items():
                    modules[m + "." + mm] = f
    return modules


def getpath():
    path = map(os.path.normcase, map(os.path.abspath, sys.path[:]))
    # get rid of site packages
    for p in path:
        if p[-13:] == "site-packages":
            def cb(p, site_package_path=os.path.abspath(p)):
                return p[:len(site_package_path)] != site_package_path
            path = filter(cb, path)
            break
    # get rid of non-existent directories and the current directory
    def cb(p, cwd=os.path.normcase(os.getcwd())):
        return os.path.isdir(p) and p != cwd
    path = filter(cb, path)
    return path


def lambda_handler(event, context):
    path = getpath()
    modules = {}

    # Remove built in modules from the list
    for m in sys.builtin_module_names:
        modules[m] = None

    for p in path:
        modules.update(getmodules(p))

    module_list = sorted(modules.keys())
    # module_list.sort()

    # filter out known test packages
    def cb(m):
        for d in TEST_PACKAGES:
            if m[:len(d)] == d:
                return 0
        return 1
    module_list = filter(cb, module_list)

    print('''# module list (generated by listmodules.py)
#
# timestamp={timestamp}
# sys.version={sys_version}
# sys.platform={sys_platform}
# platform={platform}
#
{module_list}
'''.format(
        timestamp=repr(timestamp),
        sys_version=repr(sys.version),
        sys_platform=repr(sys.platform),
        platform=repr(platform) if platform is not None else '?',
        module_list='\n'.join(module_list)
    ))

def less_verbose():
    """Show the packages installed and the versions if they have a version in the module"""
    module_dict = {name: obj for (obj, name, ispkg) in pkgutil.iter_modules() if name not in sys.builtin_module_names and name not in 'this'}
    for name in sorted(module_dict.keys()):
        ver = ''
        try:
            imported_module = importlib.import_module(name)
            ver = getattr(imported_module, '__version__', '')
        except Exception as e:
            # ver = 'exception-{}'.format(e)
            ver = ''
        print('{} {}'.format(name, ver))
@gene1wood